package 在排序数组中查找元素的第一个和最后一个位置;

public class Solution1 {
    public int[] searchRange(int[] nums, int target) {

        int low = 0;
        int high = nums.length-1;
        while(low <=high){
            int mid = low + (high-low)/2;
            if(nums[mid] == target){
                int left = mid;
                int right = mid;
                while(left ==target){
                    left--;
                }
                while (right == target){
                    right++;
                }
                return new int[]{left+1,right-1};

            }
            else if(nums[mid] < target){
                low = mid +1;
                int num = 0;
                int start = 0;
                while (nums[low] == target) {
                    start = low;
                    num++;
                }
                return new int[]{start,num-1};
            }
            else{
                high = mid -1;
                int num = 0;
                int start = 0;
                while (nums[high] == target){
                    start = high;
                    num++;
                }
                return new int[]{num+1,start};
            }

        }
        return new int[]{-1,-1};
    }
}
